<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="80px">
      <el-form-item label="用户名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--      <el-form-item label="用户id" prop="goodsId">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.goodsId"-->
      <!--          placeholder="请输入用户id"-->
      <!--          clearable-->
      <!--          size="small"-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="用户id" prop="addressId">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.addressId"-->
      <!--          placeholder="请输入用户id"-->
      <!--          clearable-->
      <!--          size="small"-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label="订单号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单状态" prop="goodsOrderStatus">
        <el-select  v-model="queryParams.goodsOrderStatus" placeholder="选择订单状态" clearable size="small">
          <el-option
            v-for="dict in sys_goods_order_status"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="快递单号" prop="expressNo">
        <el-input
          v-model="queryParams.expressNo"
          placeholder="请输入快递单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--      <el-form-item label="发货时间" prop="expressTime">-->
      <!--        <el-date-picker clearable size="small" style="width: 200px"-->
      <!--                        v-model="queryParams.expressTime"-->
      <!--                        type="date"-->
      <!--                        value-format="yyyy-MM-dd"-->
      <!--                        placeholder="选择发货时间"-->
      <!--                        @change="dataShow">-->
      <!--        </el-date-picker>-->
      <!--      </el-form-item>-->
      <el-form-item label="发货时间" >
        <div class="block">
          <el-date-picker
            v-model="dateRange"
            size="small"
            style="width: 240px"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </div>
      </el-form-item>
      <el-form-item label="支付方式" prop="payType">
        <el-select  v-model="queryParams.payType" placeholder="选择订单状态" clearable size="small">
          <el-option
            v-for="dict in sys_pay_type"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!--    <el-row :gutter="10" class="mb8">-->
    <!--      <el-col :span="1.5">-->
    <!--        <el-button-->
    <!--          type="primary"-->
    <!--          icon="el-icon-plus"-->
    <!--          size="mini"-->
    <!--          @click="handleAdd"-->
    <!--          v-hasPermi="['system:order:add']"-->
    <!--        >新增</el-button>-->
    <!--      </el-col>-->
    <!--      <el-col :span="1.5">-->
    <!--        <el-button-->
    <!--          type="success"-->
    <!--          icon="el-icon-edit"-->
    <!--          size="mini"-->
    <!--          :disabled="single"-->
    <!--          @click="handleUpdate"-->
    <!--          v-hasPermi="['system:order:edit']"-->
    <!--        >修改</el-button>-->
    <!--      </el-col>-->
    <!--      <el-col :span="1.5">-->
    <!--        <el-button-->
    <!--          type="danger"-->
    <!--          icon="el-icon-delete"-->
    <!--          size="mini"-->
    <!--          :disabled="multiple"-->
    <!--          @click="handleDelete"-->
    <!--          v-hasPermi="['system:order:remove']"-->
    <!--        >删除</el-button>-->
    <!--      </el-col>-->
    <!--      <el-col :span="1.5">-->
    <!--        <el-button-->
    <!--          type="warning"-->
    <!--          icon="el-icon-download"-->
    <!--          size="mini"-->
    <!--          @click="handleExport"-->
    <!--          v-hasPermi="['system:order:export']"-->
    <!--        >导出</el-button>-->
    <!--      </el-col>-->
    <!--    </el-row>-->

    <el-table v-loading="loading" :data="orderList">
      <!--      <el-table-column type="selection" width="55" align="center" />-->
      <!--         @selection-change="handleSelectionChange"-->
      <el-table-column label="用户名" align="center" prop="userName" />
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="商品名" align="center" prop="goodsName" />
      <el-table-column label="收货地址" align="center" prop="allAddress" />
      <el-table-column label="订单号" align="center" prop="orderNo" >
<!--        <template slot-scope="scope">-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            @click="handleOder(scope.row)"-->
<!--          >{{scope.row.orderNo}}</el-button>-->
<!--        </template>-->
      </el-table-column>
      <el-table-column label="订单状态" align="center" prop="goodsOrderStatus" :formatter="sysGoodsFormat" >
      </el-table-column>
      <el-table-column label="快递单号" align="center" prop="expressNo" />
      <el-table-column label="发货时间" align="center" prop="expressTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expressTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="退换货原因" align="center" prop="reason" />
      <el-table-column label="支付时间" align="center" prop="payTime" width="180"  >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.payTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="支付方式" align="center" prop="payType"  :formatter="sysPayFormat">
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
      <!--        <template slot-scope="scope">-->
      <!--          <el-button-->
      <!--            size="mini"-->
      <!--            type="text"-->
      <!--            icon="el-icon-edit"-->
      <!--            @click="handleUpdate(scope.row)"-->
      <!--            v-hasPermi="['system:order:edit']"-->
      <!--          >修改</el-button>-->
      <!--          <el-button-->
      <!--            size="mini"-->
      <!--            type="text"-->
      <!--            icon="el-icon-delete"-->
      <!--            @click="handleDelete(scope.row)"-->
      <!--            v-hasPermi="['system:order:remove']"-->
      <!--          >删除</el-button>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改二手物品订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="下单用户id" prop="mobileUserId">
          <el-input v-model="form.mobileUserId" placeholder="请输入下单用户id" />
        </el-form-item>
        <el-form-item label="用户id" prop="goodsId">
          <el-input v-model="form.goodsId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="用户id" prop="addressId">
          <el-input v-model="form.addressId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="订单号" prop="orderNo">
          <el-input v-model="form.orderNo" placeholder="请输入订单号" />
        </el-form-item>
        <el-form-item label="订单状态 0-待支付 1-待发货 2-待收货 3-待评价 4-已取消 5-已完成 6-退换货">
          <el-radio-group v-model="form.goodsOrderStatus">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="快递单号" prop="expressNo">
          <el-input v-model="form.expressNo" placeholder="请输入快递单号" />
        </el-form-item>
        <el-form-item label="发货时间" prop="expressTime">
          <el-date-picker clearable size="small" style="width: 200px"
                          v-model="form.expressTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择发货时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="支付方式 1-支付宝 2-微信(数据字典)" prop="reason">
          <el-input v-model="form.reason" placeholder="请输入支付方式 1-支付宝 2-微信(数据字典)" />
        </el-form-item>
        <el-form-item label="支付方式 1-支付宝 2-微信(数据字典)" prop="payTime">
          <el-date-picker clearable size="small" style="width: 200px"
                          v-model="form.payTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择支付方式 1-支付宝 2-微信(数据字典)">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="支付方式 1-支付宝 2-微信(数据字典)">
          <el-select v-model="form.payType" placeholder="请选择支付方式 1-支付宝 2-微信(数据字典)">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="订单详情" :visible.sync="orderTableShow" width="600px">
      <el-table v-loading="loading" :data="orderTableList">
        <el-table-column label="用户名" align="center" prop="userName" />
        <el-table-column label="商品名" align="center" prop="goodsName" />
        <el-table-column label="订单号" align="center" prop="orderNo" />
        <el-table-column label="金额" align="center" prop="money" />
        <el-table-column label="发货时间" align="center" prop="expressTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.expressTime) }}</span>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
  import { listOrder, getOrder, delOrder, addOrder, updateOrder, exportOrder } from "@/api/system/secondHand";

  export default {
    name: "Order",
    data() {
      return {
        //点击订单号展示的表格DIALOG
        orderTableList:[],
        orderTableShow:false,
        dateRange:[],
        pickerOptions: {
          shortcuts: [{
            text: '最近一周',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit('pick', [start, end]);
            }
          }]},
        //二手物品支付方式
        sys_pay_type:[],
        //二手商品订单状态
        sys_goods_order_status:[],
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 二手物品订单表格数据
        orderList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          mobileUserId: undefined,
          goodsId: undefined,
          addressId: undefined,
          orderNo: undefined,
          goodsOrderStatus: undefined,
          expressNo: undefined,
          expressTime: undefined,
          reason: undefined,
          payTime: undefined,
          payType: undefined,
          phone:undefined
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        }
      };
    },
    watch:{
      'dateRange':{
        handler: function(newVal, oldVal) { //操作者
          console.log(newVal)
          this.queryParams.beginTime = newVal[0]
          this.queryParams.endTime = newVal[1]
        },
      }
    },
    created() {

      //二手物品支付方式
      this.getDicts("sys_pay_type").then(response => {
        console.log(response)
        this.sys_pay_type = response.data;
      });

      //二手商品订单状态
      this.getDicts("sys_goods_order_status").then(response => {
        console.log(response)
        this.sys_goods_order_status = response.data;
      });

      this.getList();
    },
    methods: {
      //获取订单表格
      handleOder(row){
        this.orderTableList.length = 0
        var goodsOrderId = row.goodsOrderId
        this.orderTableShow = true
        getOrder(goodsOrderId).then(res=>{
          console.log('订单表格返回值',res.data)
          res.data.allAddress = res.data.area  +  res.data.address
          var obj = res.data
          this.orderTableList.push(obj)
        })
      },

      dataShow(e){
        console.log(e)
      },
      // 字典状态字典翻译
      sysPayFormat(row, column) {
        return this.selectDictLabel(this.sys_pay_type, row.payType);
      },
      sysGoodsFormat(row, column) {
        return this.selectDictLabel(this.sys_goods_order_status, row.goodsOrderStatus);
      },
      /** 查询二手物品订单列表 */
      getList() {
        this.loading = true;
        console.log('参数',this.queryParams)
        // '二手物品订单列表',
        listOrder(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          console.log(response)
          response.rows.forEach((items,i)=>{
            items.allAddress = (!!items.cityName?items.cityName:'') + (!!items.address?items.address:'')
          })
          this.orderList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          goodsOrderId: undefined,
          mobileUserId: undefined,
          goodsId: undefined,
          addressId: undefined,
          orderNo: undefined,
          goodsOrderStatus: "0",
          expressNo: undefined,
          expressTime: undefined,
          reason: undefined,
          payTime: undefined,
          payType: undefined,
          remark: undefined,
          createBy: undefined,
          createTime: undefined,
          updateBy: undefined,
          updateTime: undefined,
          phone:undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.goodsOrderId)
        this.single = selection.length!=1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加二手物品订单";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const goodsOrderId = row.goodsOrderId || this.ids
        getOrder(goodsOrderId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改二手物品订单";
        });
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.goodsOrderId != undefined) {
              updateOrder(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.msg);
                }
              });
            } else {
              addOrder(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.msg);
                }
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const goodsOrderIds = row.goodsOrderId || this.ids;
        this.$confirm('是否确认删除二手物品订单编号为"' + goodsOrderIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delOrder(goodsOrderIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有二手物品订单数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportOrder(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
      }
    }
  };
</script>
